<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文物展览系统 - 登录</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            height: 100vh;
            font-family: "Microsoft YaHei", serif;
            background-image: url(/assets/images/pic1.jpg);
            background-repeat: no-repeat;
            background-position: center center;
            background-size: cover;
            background-attachment: fixed;
        }

        .login-container {
            width: 420px;
            margin: 120px auto;
            margin-left: calc(50% - 210px + 50px);
            padding: 40px;
            background-color: rgba(255, 255, 255, 0.95);
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        }

        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: #8C1515; /* 文物色调 */
        }

        .login-title h3 {
            font-size: 24px;
            font-weight: bold;
        }

        .input-group {
            margin-bottom: 25px;
            position: relative;
        }

        .input-group i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #8C1515;
        }

        .input-group input {
            width: 100%;
            height: 45px;
            padding-left: 45px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        .input-group input:focus {
            outline: none;
            border-color: #8C1515;
        }

        .error-message {
            color: #dc3545;
            text-align: center;
            margin-bottom: 15px;
            height: 20px;
        }

        .login-btn {
            width: 100%;
            height: 45px;
            background-color: #8C1515;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 10px;
        }

        .login-btn:hover {
            background-color: #6D1111;
        }

        .link-area {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            font-size: 14px;
        }

        .link-area a {
            color: #8C1515;
            text-decoration: none;
        }

        .link-area a:hover {
            text-decoration: underline;
        }

        .login-footer {
            text-align: center;
            margin-top: 30px;
            font-size: 12px;
            color: #666;
        }

        @media (max-width: 500px) {
            .login-container {
                width: 90%;
                padding: 30px 20px;
                margin-left: calc(50% - 45% + 50px);
            }
        }
    </style>
</head>

<body>

<div class="login-container">
    <div class="login-title">
        <h3>文物展览系统</h3>
    </div>

    <form id="loginForm" th:action="@{/user/login}" method="POST">
        <div class="error-message" id="errorMessage"></div>

        <div class="input-group">
            <i class="fa fa-user"></i>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>

        <div class="input-group">
            <i class="fa fa-lock"></i>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>

        <button type="submit" class="login-btn">登录</button>

        <div class="link-area">
            <a th:href="@{/forgotPassword}">忘记密码?</a>
            <a th:href="@{/t/register}">注册账号</a>
        </div>
    </form>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('errorMessage');

            // 发送登录请求
            fetch('/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password)
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 登录成功，跳转到主页
                        window.location.href = '/';
                    } else {
                        // 登录失败，显示错误信息
                        errorMessage.textContent = data.message;
                    }
                })
                .catch(error => {
                    console.error('登录出错:', error);
                    errorMessage.textContent = '登录失败，请重试';
                });
        });
    </script>

    <div class="login-footer">
        © 2025 文物展览系统 版权所有
    </div>
</div>
</body>
</html>